﻿var showMessage = function (request, msg) {
    var temp = eval("(" + request.responseText + ")");
    alert(msg + "\nDetail: " + temp.Message);
};

if (!String.prototype.jsonDate) {
    String.prototype.jsonDate = function () {
        if (this == null) return "";
        var dt = new Date(parseInt(this.replace("/Date(", "").replace(")/", ""), 10));
        return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
    }
}

var handlerModule = (function () {

    var requestUrl = "/Handler/RequestService.asmx/";

    return {

        loadAnalytics: function (fromDate, toDate, chartCate) {
            var result;
            $.ajax({
                type: "POST",
                url: requestUrl + 'LoadAnalytics',
                data: JSON.stringify({ fromDate: fromDate, toDate: toDate, chartCate: chartCate }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function Success(data, status) {
                    result = data.d;
                },
                error: function (xmlHttpRequest) {
                    showMessage(xmlHttpRequest, 'failed');
                    return false;
                }
            });
            return result;
        },
        loadTopCategory: function (fromDate, toDate, chartCate) {
            var result;
            $.ajax({
                type: "POST",
                url: requestUrl + 'LoadTopCategory',
                data: JSON.stringify({ fromDate: fromDate, toDate: toDate, chartCate: chartCate }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function Success(data, status) {
                    result = data.d;
                },
                error: function (xmlHttpRequest) {
                    showMessage(xmlHttpRequest, 'failed');
                    return false;
                }
            });
            return result;
        },
        loadAllCategory: function (fromDate, toDate, chartCate) {
            var result;
            $.ajax({
                type: "POST",
                url: requestUrl + 'LoadAllCategory',
                data: JSON.stringify({ fromDate: fromDate, toDate: toDate, chartCate: chartCate }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function Success(data, status) {
                    result = data.d;
                },
                error: function (xmlHttpRequest) {
                    showMessage(xmlHttpRequest, 'failed');
                    return false;
                }
            });
            return result;
        }
    };

})();